package leetcode;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0503.java
 * @Author : WangRuoyu
 * @Date 2023/11/4 21:35
 */


public class Q0503 {
    public int[] nextGreaterElements(int[] nums) {
        int[] res = new int[nums.length];
        Arrays.fill(res, 0x3f3f3f3f);
        ArrayList<Integer> big = new ArrayList<>();
        big.add(0x3f3f3f3f);
        for (int i = nums.length - 1; i >= 0; i--) {
            while (nums[i] >= big.get(big.size() - 1)) big.remove(big.size() - 1);
            big.add(nums[i]);
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            while (nums[i] >= big.get(big.size() - 1)) big.remove(big.size() - 1);
            int k = big.get(big.size() - 1);
            res[i] = k == 0x3f3f3f3f ? -1 : k;
            big.add(nums[i]);
        }
        return res;
    }

    public static void main(String[] args) {
        Q0503 sl = new Q0503();
        int[] nums = {1, 2, 1};
        System.out.println(Arrays.toString(sl.nextGreaterElements(nums)));
    }
}